package passwordapp;

/**
 * Copyright [2011] [Raymond Pankhurst]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author rpankhurst
 */
public class PasswordEntryForm implements CommandListener {
    public Command UPDATE_COMMAND = new Command("Updated", Command.ITEM, 2);
    
    private MIDlet m_Parent     = null;
    private PasswordFormListener m_Listener = null;
    private PasswordEntry m_Entry   = null;
//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Form form;
    private TextField txtDesc;
    private TextField txtUser;
    private TextField txtPW;
    private TextField txtNotes;
    private Command backCommand;
//</editor-fold>//GEN-END:|fields|0|
    /**
     * The PasswordEntryForm constructor.
     * @param midlet the midlet used for getting
     */
    public PasswordEntryForm(MainMenu mid) {
        this.m_Parent = mid;
        this.m_Listener = mid;
    }
    public void show() {
        switchDisplayable(null, getForm());
    }
    public void setPasswordEntry(PasswordEntry ent) {
        m_Entry = ent;
        getTxtDesc().setString(ent.Description);
        getTxtPW().setString(ent.Password);
        getTxtUser().setString(ent.Username);
        getTxtNotes().setString(ent.Notes);      
    }
//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
//</editor-fold>//GEN-END:|methods|0|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize

    /**
     * Initializes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|2-switchDisplayable|0|2-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|2-switchDisplayable|0|2-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|2-switchDisplayable|1|2-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|2-switchDisplayable|1|2-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|2-switchDisplayable|2|
//</editor-fold>//GEN-END:|2-switchDisplayable|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|4-commandAction|0|4-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|4-commandAction|0|4-preCommandAction
        // write pre-action user code here
        if (displayable == form) {//GEN-BEGIN:|4-commandAction|1|17-preAction
            if (command == backCommand) {//GEN-END:|4-commandAction|1|17-preAction
                // write pre-action user code here
                if(getTxtDesc().getString().length() == 0) {
                    Alert alt = new Alert("Error");
                    alt.setString("Please fill in at least the description");
                    switchDisplayable(null, alt);
                } else {
                    m_Entry.Password = getTxtPW().getString();
                    m_Entry.Username = getTxtUser().getString();
                    m_Entry.Description = getTxtDesc().getString();
                    m_Entry.Notes = getTxtNotes().getString();
                    m_Listener.notifyUpdate(m_Entry);
                }
//GEN-LINE:|4-commandAction|2|17-postAction
                // write post-action user code here
            }//GEN-BEGIN:|4-commandAction|3|4-postCommandAction
        }//GEN-END:|4-commandAction|3|4-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|4-commandAction|4|
//</editor-fold>//GEN-END:|4-commandAction|4|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">//GEN-BEGIN:|11-getter|0|11-preInit
    /**
     * Returns an initiliazed instance of form component.
     * @return the initialized component instance
     */
    public Form getForm() {
        if (form == null) {//GEN-END:|11-getter|0|11-preInit
            // write pre-init user code here
            form = new Form("Entry", new Item[]{getTxtDesc(), getTxtUser(), getTxtPW(), getTxtNotes()});//GEN-BEGIN:|11-getter|1|11-postInit
            form.addCommand(getBackCommand());
            form.setCommandListener(this);//GEN-END:|11-getter|1|11-postInit
            // write post-init user code here
        }//GEN-BEGIN:|11-getter|2|
        return form;
    }
//</editor-fold>//GEN-END:|11-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: txtDesc ">//GEN-BEGIN:|12-getter|0|12-preInit
    /**
     * Returns an initiliazed instance of txtDesc component.
     * @return the initialized component instance
     */
    public TextField getTxtDesc() {
        if (txtDesc == null) {//GEN-END:|12-getter|0|12-preInit
            // write pre-init user code here
            txtDesc = new TextField("Description", null, 32, TextField.ANY);//GEN-LINE:|12-getter|1|12-postInit
            // write post-init user code here
        }//GEN-BEGIN:|12-getter|2|
        return txtDesc;
    }
//</editor-fold>//GEN-END:|12-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: txtUser ">//GEN-BEGIN:|13-getter|0|13-preInit
    /**
     * Returns an initiliazed instance of txtUser component.
     * @return the initialized component instance
     */
    public TextField getTxtUser() {
        if (txtUser == null) {//GEN-END:|13-getter|0|13-preInit
            // write pre-init user code here
            txtUser = new TextField("Username", null, 32, TextField.ANY);//GEN-LINE:|13-getter|1|13-postInit
            // write post-init user code here
        }//GEN-BEGIN:|13-getter|2|
        return txtUser;
    }
//</editor-fold>//GEN-END:|13-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: txtPW ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of txtPW component.
     * @return the initialized component instance
     */
    public TextField getTxtPW() {
        if (txtPW == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            txtPW = new TextField("PW", null, 32, TextField.ANY);//GEN-LINE:|14-getter|1|14-postInit
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return txtPW;
    }
//</editor-fold>//GEN-END:|14-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: txtNotes ">//GEN-BEGIN:|15-getter|0|15-preInit
    /**
     * Returns an initiliazed instance of txtNotes component.
     * @return the initialized component instance
     */
    public TextField getTxtNotes() {
        if (txtNotes == null) {//GEN-END:|15-getter|0|15-preInit
            // write pre-init user code here
            txtNotes = new TextField("Notes", null, 32, TextField.ANY);//GEN-LINE:|15-getter|1|15-postInit
            // write post-init user code here
        }//GEN-BEGIN:|15-getter|2|
        return txtNotes;
    }
//</editor-fold>//GEN-END:|15-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|16-getter|0|16-preInit
    /**
     * Returns an initiliazed instance of backCommand component.
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|16-getter|0|16-preInit
            // write pre-init user code here
            backCommand = new Command("Back", Command.BACK, 0);//GEN-LINE:|16-getter|1|16-postInit
            // write post-init user code here
        }//GEN-BEGIN:|16-getter|2|
        return backCommand;
    }
//</editor-fold>//GEN-END:|16-getter|2|
    
    private Display getDisplay() {
        return Display.getDisplay(this.m_Parent);
    }

}
